package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataActionToggle extends DataAction implements ITag{
	public static String DEFAULT_ID="_ActionToggle";
		DataState state = new DataState(false);
public DataActionToggle(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.STATE))
		{
			state = new DataState(false);
			state.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_TOGGLE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.STATE))
		{
			state.copyFromgrfxML(element);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_TOGGLE,""+element.getStartLineNo()} );
		}
	}
}
public DataAction getClone() {

	DataActionToggle newActionToggle = new DataActionToggle(parent);

	super.getClonedElements(newActionToggle);

	newActionToggle.state = state.getClone();

	return newActionToggle;
}
public DataAction getClone(String path) {

	DataActionToggle newActionToggle = new DataActionToggle(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionToggle );

	super.getClonedElements(path,newActionToggle);

	newActionToggle.state = state.getClone(path);

	return newActionToggle;
}
public DataState getState() {
	return state;
}
/**
 * process method comment.
 */
protected void process()
{
 try{
	if (start.getValue() == true || end.getValue() == true) {
			state.setValue(!state.getValue(), this);
		}
 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(state,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	replace(oldObj,newObj,false);

}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(state==oldObj)
		state=(DataState)newObj;

}
public void resetChanged() {
	super.resetChanged();
	state.resetChanged();
}
protected void setState(DataState newState) {
	state = newState;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += state.togrfxML(Token.STATE);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
